
// Represents the data needed for an entity on the client side.
// @param objId The ObjectNode ID.
// @param id The entity ID.
function Entity(objId, id, scope) {
	this.objId = objId;
	this.id = id;
	this.scope = scope;
}

Entity.prototype.getId = function() {
	if (this.id && (this.id != ""))
		return this.objId + "#" + this.id;
	return this.objId;
}

Entity.prototype.load = function(onComplete) {
	if (!this.editors) {
		this.editors = new Object();
		var res = new Job(BasePath.get() + EDIT_BASE_PATH + "/EntityInfo.view", null, StatusDisplay
				.getInstance());
		res.addData("objId", this.objId);
		res.addData("id", this.id);
		res.addData("scope", this.scope);
		if (this.parentId && this.tmpl) {
			res.addData("parentId", this.parentId);
			res.addData("tmplObjId", this.tmpl.objId);
			res.addData("tmplId", this.tmpl.id);
			for (var i = 0; i < this.tmpl.args.length; i++)
				res.addData("tmplArg" + (i + 1), this.tmpl.args[i]);
		}
		res._entity = this;
		res._callback = onComplete;
		res.onSuccess = function() {
			var nodes = this.dom.documentElement.childNodes;
			for (var i = 0; i < nodes.length; ++i) {
				var n = nodes[i];
				var tagName = (n.localName)?n.localName:n.nodeName;
				if ("editor" == tagName) {
					var id = n.getAttribute("id");
					if (!id)
						id = "--default--";
					this._entity.editors[id] = n;
				}
				if ("menu" == tagName)
					this._entity.menu = n;
			}
			this._callback.call(this._entity);
			this.statusDisplay.show("...entity info loaded", null, true);
		};
		res.errorMessage = "Could not load entity info!";
		res.start();
	}
	else {
		onComplete.call(this);
	}
}

Entity.prototype.fillEditor = function(headerElement, contentElement, editorId) {
	contentElement.innerHTML = "";
	var doc = contentElement.ownerDocument;
	if (!editorId)
		editorId = "--default--";
	var editor = this.editors[editorId];
	if (editor) {
		var label = editor.getAttribute("label");
		if (!label)
			label = "Edit";
		headerElement.innerHTML = "<span>" + label + "</span> " + headerElement.innerHTML;
		contentElement.innerHTML = Dom(editor).serializeContent();
	}
	else {
		contentElement.innerHTML = "<p style='color: red;'>Editor '" + editorId
			+ "' is not available!</p>";
	}
}
